package cn.nubia.admin.application.controller;

import cn.nubia.admin.application.bean.*;
import cn.nubia.admin.application.query.SoftTagQueryParam;
import cn.nubia.admin.application.service.TagService;
import cn.nubia.admin.application.service.TagSoftMapService;
import cn.nubia.admin.common.mybatis.bean.Paged;
import cn.nubia.admin.common.springmvc.Response;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;

/**
 * 标签页面控制器.
 *
 * @author gcj
 */
@Controller
@RequestMapping(value = "/admin/softTag")
public class TagController {

    @Autowired
    private TagService tagService;
    @Autowired
    private TagSoftMapService tagSoftMapService;


    /***
     * 标签列表
     *
     * @return
     */
    @RequiresPermissions(value = "soft:app:softtag:list")
    @RequestMapping(value = "softTagList", method = RequestMethod.GET)
    public ModelAndView tagList(SoftTagQueryParam tagQuery) {
        ModelAndView modelAndView = new ModelAndView("/tag/tagList");
        Paged<SoftTagVo> tagPaged = tagService.findPageList(tagQuery);
        modelAndView.addObject("query", tagQuery);
        modelAndView.addObject("softTagPage", tagPaged);
        return modelAndView;
    }

    @RequestMapping(value = "viewTag", method = RequestMethod.GET)
     public ModelAndView viewTag(Integer tagId) {
        ModelAndView modelAndView = new ModelAndView("/tag/tagForm");
        SoftTag tag = new SoftTag();
        List<SoftTagMap> tagSoftMaps = new ArrayList<>();
        if (tagId != null) {
            tag = tagService.get(tagId);
            tagSoftMaps = tagSoftMapService.listSoftTagMaps(tagId);
        }

        modelAndView.addObject("softTag", tag);
        modelAndView.addObject("tagId", tagId);
        modelAndView.addObject("softMaps",tagSoftMaps);

        return modelAndView;
    }

    @RequestMapping(value = "viewTagSoftMap", method = RequestMethod.GET)
    public ModelAndView viewTagSoftMap(Integer id,Integer tagId) {
        ModelAndView modelAndView = new ModelAndView("/tag/choiceSoft");
        SoftTag tag = new SoftTag();
        SoftTagMap tagSoftMap = new SoftTagMap();

        if(id!=null){
            tagSoftMap = tagSoftMapService.get(id);
            tag = tagService.get(tagSoftMap.getTagId());
        }else{
            if(tagId!=null)tag = tagService.get(tagId);
        }
        modelAndView.addObject("softTag", tag);
        modelAndView.addObject("softMap",tagSoftMap);
        return modelAndView;
    }

    @RequestMapping(value = "listTagSoftMap", method = RequestMethod.GET)
    public ModelAndView listTagSoftMap(Integer tagId) {
        ModelAndView modelAndView = new ModelAndView("/tag/softmaplist");
        SoftTag tag = new SoftTag();
        SoftTagMap tagSoftMap = new SoftTagMap();
        List<SoftTagMap> tagSoftMaps = new ArrayList<>();
        if (tagId != null) {
            tag = tagService.get(tagId);
            tagSoftMaps = tagSoftMapService.listSoftTagMaps(tagId);
        }
        modelAndView.addObject("softTag", tag);
        modelAndView.addObject("softMaps",tagSoftMaps);
        return modelAndView;
    }


    @ResponseBody
    @RequestMapping(value = "getTagList", method = RequestMethod.GET)
    public Paged<SoftTagVo> getTagList(SoftTagQueryParam queryParam) {
        Paged<SoftTagVo> tagPaged = tagService.findPageList(queryParam);
        return tagPaged;
    }

    @ResponseBody
    @RequestMapping(value = "saveTag",method = RequestMethod.POST)
    public Response<Boolean> saveTag(SoftTag tag) {
        tagService.save(tag);
        return Response.getSuccessResponse(true);
    }


    @ResponseBody
    @RequestMapping(value = "deleteTag")
    public Response<Boolean> deleteTag(Integer tagId){
        tagService.delete(tagId);
        return Response.getSuccessResponse(true);
    }



    @ResponseBody
    @RequestMapping(value = "saveSoftMap")
    public Response<Boolean> saveSoftMap(SoftTagMap softMap){
        boolean result = tagSoftMapService.saveSoftMap(softMap);
        return Response.getSuccessResponse(result);
    }
    @ResponseBody
    @RequestMapping(value = "deleteSoftMap")
    public Response<Boolean> deleteSoftMap(Integer id){
        boolean result = tagSoftMapService.deleteSoftMap(id);
        return Response.getSuccessResponse(result);
    }


}
